package it.tcon.xbeedriver;


import org.apache.log4j.Logger;

import it.tcon.xbeedriver.resmanager.AtResponseManager;
import it.tcon.xbeedriver.resmanager.RemoteAtCommandManager;
import it.tcon.xbeedriver.resmanager.ZNetIoSampleManager;
import it.tcon.xbeedriver.resmanager.ZNetNodeIdentifierManager;
import com.rapplogic.xbee.api.AtCommandResponse;
import com.rapplogic.xbee.api.XBeeResponse;
import com.rapplogic.xbee.api.zigbee.ZNetNodeIdentificationResponse;
import com.rapplogic.xbee.api.zigbee.ZNetRemoteAtResponse;
import com.rapplogic.xbee.api.zigbee.ZNetRxIoSampleResponse;
import com.rapplogic.xbee.api.zigbee.ZNetRxResponse;
import com.rapplogic.xbee.api.zigbee.ZNetTxStatusResponse;
import com.rapplogic.xbee.util.ByteUtils;

public class XBeeReceiveController implements Runnable {
	
	private Logger logger = Logger.getLogger(XBeeReceiveController.class);
	private XBeeResponse response;
	private XBeeNodeList list;
	public XBeeReceiveController(XBeeResponse response,XBeeNodeList list){
		this.list = list;
		this.response = response;
	}
	
	public void run(){
		checkForApiId();
	}
	
	/**
	 * Check the ApiId for sending the response to the correct response manager
	 */
	private void checkForApiId(){
		logger.debug("Response api: "+this.response.getApiId());
		switch(this.response.getApiId()){
			case XBeeResponse.AT_RESPONSE:
				new AtResponseManager((AtCommandResponse)this.response,this.list);
				break;
			case XBeeResponse.ERROR_RESPONSE:
				//TODO
				break;
			case XBeeResponse.ZNET_IO_NODE_IDENTIFIER_RESPONSE:
				new ZNetNodeIdentifierManager((ZNetNodeIdentificationResponse)this.response,this.list);
				break;
			case XBeeResponse.ZNET_IO_SAMPLE_RESPONSE:
				logger.debug("ZNET IO SAMPLE response from: "+ByteUtils.toBase16(((ZNetRxIoSampleResponse) response).getRemoteAddress64().getAddress()));
				new ZNetIoSampleManager((ZNetRxIoSampleResponse)this.response,this.list);
				break;
			case XBeeResponse.MODEM_STATUS_RESPONSE:
				//TODO
				break;
			case XBeeResponse.ZNET_REMOTE_AT_RESPONSE:
				logger.debug("ZNET Remote AT response from: "+ByteUtils.toBase16(((ZNetRemoteAtResponse) response).getRemoteAddress64().getAddress()));
				new RemoteAtCommandManager((ZNetRemoteAtResponse)this.response,this.list);
				break;
			case XBeeResponse.ZNET_RX_RESPONSE:
				logger.debug("ZNET RX response from: "+ByteUtils.toBase16(((ZNetRxResponse) response).getRemoteAddress64().getAddress()));

				//TODO
				break;
			case XBeeResponse.RX_16_IO_RESPONSE:
				//TODO
				break;
			case XBeeResponse.RX_16_RESPONSE:
				//TODO
				break;
			case XBeeResponse.RX_64_IO_RESPONSE:
				//TODO
				break;
			case XBeeResponse.RX_64_RESPONSE:
				//TODO
				break;
			case XBeeResponse.TX_16_STATUS_RESPONSE:
				
				//TODO
				break;
			case XBeeResponse.ZNET_TX_STATUS_RESPONSE:
				logger.debug("TX status response");
				logger.debug("data:"+((ZNetTxStatusResponse)response));
				break;
			default:
				//TODO
				break;
		}
	}
	
	

	
	
	
	
}
